import BaseApp from '../BaseApp';
import apicloud from '@/config/apicloud';
import ScrollUtils from '@/lib/utils/ScrollUtils';
import PathUtils from '@/lib/utils/PathUtils';

export default abstract class BaseRefreshApp extends BaseApp {
    // 刷新完成
    private mRefreshed = true;
    // 是否锁住下拉刷新
    private mLockRefresh = false;
    public abstract onRefresh(): void;
    /**
     * 设置下拉刷新
     * @param bgColor 
     */
    public setRefreshHeaderInfo(bgColor: string = '#ffffff') {
        const load = apicloud.REFRESH_HEADER.load.map((url) => PathUtils.resolve(url));
        const pull = PathUtils.resolve(apicloud.REFRESH_HEADER.load[apicloud.REFRESH_HEADER.defaultIndex]);
        api.setCustomRefreshHeaderInfo({
            bgColor,
            isScale: true,
            image: {
                pull: api.systemType === 'ios' ? [pull, pull, pull, pull, pull, pull, pull, pull, pull, pull] : [pull],
                load
            },
            loadAnimInterval: apicloud.REFRESH_HEADER.animationStep
        }, () => {
            if (api.connectionType === 'none') {
                this.refreshFail();
            }
            this.emitRefresh();
        });
    }
    /**
     * 触发下拉刷新
     */
    public emitRefresh() {
        if (!this.mRefreshed) {
            return;
        }
        this.$emitter.onSystem({
            name: "offline" as any
        }, this.refreshFail);
        this.mRefreshed = false;
        this.onRefresh();

    }

    /**
     * 下拉结束
     */
    public refreshHeaderLoadDone() {
        this.$emitter.un({
            name: "offline" as any
        }, this.refreshFail);
        this.mRefreshed = true;
        api.refreshHeaderLoadDone();
    }
    /**
     * 刷新失败
     */
    public refreshFail(msg = '刷新失败') {
        this.refreshHeaderLoadDone();
        this.$apicloud.toast(msg);

    }

    /**
     * 设置是否锁定下拉刷新
     * @param lock 
     */
    public setLockRefresh(lock: boolean) {
        if (this.mLockRefresh === lock || api.systemType === 'ios') {
            return;
        }
        this.mLockRefresh = lock;
        const isFrm = !!api.frameName;
        if (isFrm) {
            api.setFrameAttr({
                bounces: !lock
            });
        } else {
            api.setWinAttr({
                bounces: !lock
            });
        }
    }

    /**
     * 判断是否下拉刷新完成
     */
    public isRefreshed() {
        return this.mRefreshed;
    }

}
